﻿// File: MainWindow.xaml.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace WpfPTest
{
    public partial class MainWindow : Window
    {
        private const string ProductListFilePath = @"conf\productlist.txt";
        private const string CurrentProductFilePath = @"conf\currentproduct.txt";
        public MainWindow()
        {
            InitializeComponent();
            LoadProductList();
            LoadCurrentProduct();
            // 添加这一行以确保 UpdateMainContent 在窗口加载时被调用
            Loaded += (s, e) => UpdateMainContent();
           
        }

        private void LoadProductList()
        {
            if (File.Exists(ProductListFilePath))
            {
                string[] products = File.ReadAllLines(ProductListFilePath);
                ProductComboBox.ItemsSource = products;
            }
        }

        private void LoadCurrentProduct()
        {
            if (File.Exists(CurrentProductFilePath))
            {
                string currentProduct = File.ReadAllText(CurrentProductFilePath);
                ProductComboBox.SelectedItem = currentProduct;
            }
        }

        private void SaveCurrentProduct()
        {
            string selectedProduct = ProductComboBox.SelectedItem as string;
            if (!string.IsNullOrEmpty(selectedProduct))
            {
                File.WriteAllText(CurrentProductFilePath, selectedProduct);
            }
        }
        private void OnProductSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // 保存选择到应用程序设置中
            SaveCurrentProduct();
            UpdateMainContent();
        }

        private void OnTestModeChanged(object sender, RoutedEventArgs e)
        {
            UpdateMainContent();
        }

        private void UpdateMainContent()
        {
            // 这里检查 MainContentGrid 是否为 null，以避免 NullReferenceException
            if (MainContentGrid == null) return;

            MainContentGrid.Children.Clear();
            var selectedProduct = (ProductComboBox.SelectedItem as string);
            if (selectedProduct == null) return;

            if (SingleTestRadioButton.IsChecked == true)
            {
                MainContentGrid.RowDefinitions.Clear();
                MainContentGrid.ColumnDefinitions.Clear();
                //var control = new ProductInfoControl { ProductName = selectedProduct, ProductIndex = Convert.ToString(1) };
                //MainContentGrid.Children.Add(control);
                AddProductInfoControl(0, 0, selectedProduct);
            }
            else
            {
                MainContentGrid.RowDefinitions.Clear();
                MainContentGrid.ColumnDefinitions.Clear();
                for (int i = 0; i < 3; i++)
                {
                    MainContentGrid.RowDefinitions.Add(new RowDefinition());
                    MainContentGrid.ColumnDefinitions.Add(new ColumnDefinition());
                }

                for (int i = 0; i < 9; i++)
                {
                    //var control = new ProductInfoControl { ProductName = selectedProduct, ProductIndex = Convert.ToString(i + 1) };
                    //Grid.SetRow(control, i / 3);
                    //Grid.SetColumn(control, i % 3);
                    //MainContentGrid.Children.Add(control);
                    AddProductInfoControl(i / 3, i % 3, selectedProduct);
                }
            }
        }

        private void OnSendClick(object sender, RoutedEventArgs e)
        {
           // var message = $"主窗口消息发送于 {DateTime.Now}";
            foreach (var child in MainContentGrid.Children)
            {
                if (child is ProductInfoControl control)
                {
                    var position = $"{Grid.GetRow(control)},{Grid.GetColumn(control)}";
                    control.Position = position;  // 设置控件的位置
                    var message = $"消息发送于 {DateTime.Now} (位置: {position})";
                    control.ReceiveMessage(message);
                }
            }
        }

        private void AddProductInfoControl(int row, int column, string productName)
        {
            var control = new ProductInfoControl { ProductName = productName };
            var position = $"{row},{column}";
            control.Position = position;  // 确保在添加 MessageSent 事件处理程序之前设置 Position 属性
            control.MessageSent += OnProductControlMessageSent;
            Grid.SetRow(control, row);
            Grid.SetColumn(control, column);
            MainContentGrid.Children.Add(control);
        }

        private void OnProductControlMessageSent(string message)
        {
            ReceivedMessagesTextBox.Text += $"{message}\n";
        }
    }
}
